﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCDto.Trading.SaleGoods.SaleGoodsCategory;
using BCDto.Trading.SaleShoppingCart;
using BCService.Trading.SaleShoppingCart;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;

namespace BCTMSRESTful.Controllers.Anonymous
{
    [Route("api/v1/[controller]")]
    [ApiController]
    public class ShoppingCartController : WebsiteBaseController
    {
        private readonly ISaleShoppingCartService saleShoppingCartService;

        public ShoppingCartController(ISaleShoppingCartService saleShoppingCartService)
        {
            this.saleShoppingCartService = saleShoppingCartService;
        }

        #region 添加销售购物车
        /// <summary>
        ///  添加销售购物车
        /// </summary>
        /// <param name="requestDto">销售购物车信息</param>
        /// <response code="201">添加销售购物车成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加销售购物车")]
        [ProducesResponseType(typeof(SaleShoppingCartDto), 201)]
        public IActionResult Post(
            [FromBody]SaleShoppingCartRequestDto requestDto)
        {
            var result = this.saleShoppingCartService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 获取购物车列表
        /// <summary>
        ///  获取购物车列表
        /// </summary>
        /// <response code="200">获取购物车列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<SaleShoppingCartDto>), 200)]
        public IActionResult ShoppingCart()
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleShoppingCartService.GetList(this.UserInfo.UserId));
        }
        #endregion

        #region 获取销售购物车总数
        /// <summary>
        ///  获取销售购物车总数
        /// </summary>
        /// <response code="200">获取用户地址总数</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(int), 200)]
        public IActionResult Count()
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleShoppingCartService.Count(this.UserInfo.UserId));
        }
        #endregion

        #region 修改购物车商品数量
        /// <summary>
        ///  修改购物车商品数量
        /// </summary>
        /// <param name="saleShoppingCartId">购物车Id</param>
        /// <param name="saleCount">商品数量</param>
        /// <response code="200">修改购物车商品数量</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{shopping_cart_id}/[action]/{sale_count}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult Count([FromRoute(Name ="shopping_cart_id")]long saleShoppingCartId,
            [FromRoute(Name = "sale_count")]int saleCount)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleShoppingCartService.UpdateSaleCount(saleShoppingCartId,saleCount));
        }
        #endregion

        #region 删除购物车商品
        /// <summary>
        ///  删除购物车商品
        /// </summary>
        /// <param name="saleShoppingCartId">购物车Id</param>
        /// <response code="200">修改购物车商品数量</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{shopping_cart_id}")]
        [Log("删除销售购物车")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "shopping_cart_id")]long saleShoppingCartId)
        {
            this.saleShoppingCartService.Delete(saleShoppingCartId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion
        
    }
}
